package com.sxj.corejava.code02_jdbc;

import com.sxj.corejava.entity.Dept;
import com.sxj.corejava.entity.Emp;

import java.sql.*;

/**
 * @author 石小俊
 * @date 2023年12月2023/12/2日 9:44
 */
public class Test07_多表保存操作 {

    public static void main(String[] args) {
        // 由于保存的员工属于当前保存的部门
        // 因此,必须先有部门后有员工
        Dept dept = new Dept("研发部","负责研发企业产品");
        // 保存部门信息并返回主键
        Integer id = insertReturnPrimaryKey(dept);
        // 由于添加员工时需要指定当前员工所属部门
        // 因此,当返回主键后,将主键的值设置到dept对象中主键所对应的属性上
        dept.setId(id);
        // 然后将该对象交给需要保存的员工
        Emp emp = new Emp("admin",6000.0,dept);
        Emp emp2 = new Emp("alice",7000.0,dept);
        Emp emp3 = new Emp("jack",6500.0,dept);
        insertEmp(emp);
        insertEmp(emp2);
        insertEmp(emp3);
        System.out.println("添加成功");
    }

    public static Integer insertReturnPrimaryKey(Dept dept){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Integer id = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8","root","");
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_dept ")
                    .append("   (name,remark)")
                    .append(" values ")
                    .append("   (?,?)")
                    .toString();
            ps = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            ps.setString(1,dept.getName());
            ps.setString(2,dept.getRemark());
            ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            while (rs.next()){
                id = rs.getInt(1);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("未找到驱动");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("连接数据库失败");
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("释放连接出错");
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return id;
    }

    public static void insertEmp(Emp emp){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8","root","");
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_emp")
                    .append("   (name,salary,dept_id)")
                    .append(" values ")
                    .append("   (?,?,?)")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setDouble(2,emp.getSalary());
            ps.setInt(3,emp.getDept().getId());
            ps.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("未找到驱动");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("连接数据库失败");
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("释放连接出错");
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
